//创建一个新的 div 元素作为容器
const container = document.createElement('div');

// 创建一个按钮元素
const helloButton = document.createElement('button');
helloButton.textContent = '中文配音';
helloButton.style.fontSize = '16px'; // 设置按钮字体大小

var video = document.querySelector("video");
var audio = document.querySelector("#new-audio") || new Audio();
var enable = false;

const url = window.location.href;
// audio.id = "new-audio";
// audio.src = "https://audio.nndou.com/api/ted?url=" + url; // 请替换为你的音频地址
// audio.src = "https://audio.nndou.com/static/out_bba690934dc14b5cbd157de3a1377ca1.wav"
// container.appendChild(audio);

function ontimeupdate() {
    // console.log(video.currentTime);
    // var video = document.querySelector("video");
    // audio = document.querySelector("new-audio");
    if(audio){
        // ad=document.querySelectorAll(".text-textPrimary-onDark")[1].innerText;
        // console.log(ad);
        // console.log(video.currentTime);
        // video.muted = true;
        // if (video.currentTime-5>0){
        //     audio.currentTime = video.currentTime-5;
        // }else{
        //     audio.currentTime = 0;
        // }
        //
        //
        // if (video.paused) {
        //   audio.pause();
        // } else {
        //   audio.play();
        // }
    }

}

helloButton.addEventListener('click', () => {
  if(!enable){
    // const url = window.location.href;
    // audio.id = "new-audio";
    // audio.src = "https://audio.nndou.com/api/ted?url=" + url; // 请替换为你的音频地址
    // container.appendChild(audio);
    var audiourl = "https://audio.nndou.com/api/ted?url=" + url;
    if(audio.src!=audiourl){
        audio.id = "new-audio";
        audio.src = audiourl; // 请替换为你的音频地址
        // audio.src = "https://audio.nndou.com/static/out_bba690934dc14b5cbd157de3a1377ca1.wav"
        container.appendChild(audio);
    }


    helloButton.textContent = "中文配音(已开启)"
    video.addEventListener('timeupdate', ontimeupdate);
    video.muted = true
    enable = true;
  }else{
    audio.pause()
    helloButton.textContent = "中文配音"
    video.removeEventListener('timeupdate',ontimeupdate);
    video.muted = false;
    enable = false
  }
});

video.addEventListener('timeupdate', ontimeupdate)
var videoElement = document.getElementById('video').parentElement;
if (videoElement) {
    var newDiv = document.createElement('div');
    newDiv.style.height = '30px';
    newDiv.appendChild(helloButton);
    videoElement.insertAdjacentElement('afterend', newDiv);
}

document.addEventListener('DOMContentLoaded', function () {
    console.log('页面加载完成');
});